// 初始化列表
var grid_selector = "";
var $grid_selector = "";
var pager_selector = "";
var parent_column = "";
var colNames = "";
var widthFunc = "";
var colModels = "";
//=========================================================================================================================================
$(document).ready(function () {
    $('#resourceName').click(function () {
        // 初始化列表
        grid_selector = '#grid-table';
        $grid_selector = $(grid_selector);
        pager_selector = '#grid-pager';
        parent_column = $grid_selector.closest('[class*="col-"]');
        colNames = [
            '资源分类',
            '资源名称',
            '资源IP',
            'domain',
            'uuid'
        ];
        widthFunc = parent_column.width() / colNames.length;
        colModels = [
            {name: 'classNAME', index: 'classNAME', hideWidth: 300},
            {name: 'name', index: 'name', hideWidth: 300},
            {name: 'ip', index: 'ip', hideWidth: 300},
            {name: 'domain', index: 'domain', hideWidth: 300},
            {name: 'uuid', index: 'uuid', hideWidth: 300}
        ];


        // 通过该方法来为每次弹出的模态框设置最新的zIndex值，从而使最新的modal显示在最前面
        $(document).on('show.bs.modal', '.modal', function () {
            var zIndex = 1040 + (10 * $('.modal:visible').length);
            $(this).css('z-index', zIndex);
            setTimeout(function () {
                $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
            }, 0);
        }).on('shown.bs.modal', function () {// 当模态框对用户可见时触发（将等待 CSS 过渡效果完成）
            initKbpTable();
        });

        $.launchDialog('popup');
    });
    //当前页面打开窗口的尺寸
    window.onresize = function () {
        changeFrameHeight();
    }
    changeFrameHeight();
});


//=========================================================================================================================================
function changeFrameHeight() {
    var mainIframeHeight = $(window).height();
    var systemViewIframeHeight = mainIframeHeight - 72;
    $('#systemViewIframe').height(systemViewIframeHeight);
}


function isNotEmpty(obj) {
    if (obj != null && obj != '' && obj != undefined) {
        return true;
    }
    return false;
}


function initKbpTable() {
    $grid_selector.jqGrid({
        url: ctx + '/KbpResourceData/queryPageListType',
        mtype: 'post',
        datatype: 'json',
        height: '100%',
        colNames: colNames,
        colModel: colModels,
        autowidth: true,
        altRows: true,
        rownumbers: true,
        viewrecords: true,
        multiselect: true,
        multiboxonly: true,
        rowNum: rowNum,
        rowList: rowList,
        sortname: 'ciId',
        pager: pager_selector,
        sortorder: 'asc',
        jsonReader: jsonReader,
        beforeRequest: function () {
            var postData = {};
            $('#queryPanel').find('[name^="search."]').each(function () {
                var $this = $(this);
                // console.log($this.val());
                postData[$this.attr('name').replace(/search\./, '')] = $this.val();
            });
            $.extend($grid_selector.getGridParam('postData'), postData);
        },
        loadComplete: function () {
            setTimeout(function () {
                $grid_selector.jqGrid('setGridParam', parent_column.width());
            }, 20);
        }
    });

}

/*点击确认触发的操作*/
$('#confirm').click(function () {
    //获取选中行的id
    var rowIds = $grid_selector.jqGrid('getGridParam', 'selarrrow');
    if (rowIds == null || rowIds.length == 0) {
        $(this).dynamicPop('请选择一条记录!');
    } else if (rowIds.length > 1) {
        $(this).dynamicPop(' 不能选择多条记录, 请选择一条记录!');
    } else {
        var rowId = $grid_selector.jqGrid('getGridParam', 'selrow');
        var rowData = $("#grid-table").jqGrid('getRowData', rowId);
        $("#resourceName").val(rowData.name + "(" + rowData.ip + ")");
        $("#unitId").val(rowData.classNAME + ":" + rowData.domain + ":" + rowData.uuid);
        $("#classNAME").val(rowData.classNAME);
        // 最后关闭该窗口
        $('#popup').modal('hide');
    }
});


//显示隐藏查询条件
$('#searBtn').click(function () {
    $(this).find('i').toggleClass('fa-arrow-down');
    $(this).find('i').toggleClass('fa-arrow-up');
});

//查询按钮
$('#searchBtn').click(function () {
    $grid_selector.trigger('reloadGrid', [{page: 1}]);
});

// 重置查询表单
$('#resetBtn').click(function () {
    $('#queryPanel').find('input[type=text]').each(function () {
        $(this).val('');
    });
    $('#queryPanel').find('input[type=checkbox],input[type=radio]').each(function () {
        $(this).removeAttr('checked');
    });
    $grid_selector.trigger('reloadGrid', [{page: 1}]);
});


// 重置iframe输入框
$('#resetBtn1').click(function () {
    $('#input').find(':input(:text)').each(function () {
        $(this).val('');
    });
});


//查询按钮的操作
$('#searchBtn1').click(function () {
    var name = $('#resourceName').val();
    if (name == null || name.length == 0 || name == "") {
        $(this).dynamicPop('请选择资源!');
    } else {
        var kbpPath = "";
        var unitId = $('#unitId').val();
        var classNAME = $('#classNAME').val();
        var kbpCode = unitId.substr(0,6);
        if (kbpCode == 'Oracle') {
            kbpPath = '/DataBase';
        }  else if (unitId.substr(0,11) == 'LinuxServer') {
            kbpPath = '/Host';
        } else if (unitId.substr(0,12) == 'IBMAixServer'){
            kbpPath = '/Host';
        } else if (unitId.substr(0,7) == 'Windows'){
            kbpPath = '/Host';
        } else if (unitId.substr(0,4) == 'Line'){
            kbpPath = '/Host';
        } else if (unitId.substr(0,9) == 'UPSDevice'){
            kbpPath = '/Host';
        } else if (unitId.substr(0,8) == 'Weblogic'){
            kbpPath = '/MiddleWare';
        } else if (unitId.substr(0,13) == 'NetworkDevice'){
            kbpPath = '/Host';
        } else if (unitId.substr(0,4) == 'HTTP'){
            kbpPath = '/MiddleWare';
        }
        var url = ctx + kbpPath + "/" + unitId + "/" + classNAME;
        $('#systemViewIframe').attr('src', url);
    }
});

// 刷新JqGrid列表
function reloadJqGrid(datatype) {
    $grid_selector.jqGrid('setGridParam', {datatype: datatype}).trigger('reloadGrid', [{page: 1}]);
}
